package com.harveyspace.aes.core.response;

import com.google.gson.annotations.SerializedName;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用响应基类
 *
 * @Author <a href="harveyer@163.com">harvey</a>
 * @date 2016/7/18
 * @since 1.0.0
 */
public class BaseResponse<T> {

    /**
     * 响应业务码
     */
    protected Integer code;

    /**
     * 响应信息
     */
    protected String message;

    /**
     * 响应数据
     */
    protected T data;

    /**
     * 响应数据
     */
    @SerializedName("extra_param")
    protected Object extraParam;

    /**
     * 构造 BasePageResponse 的静态方法
     *
     * <pre>
     * BaseResponse.builder().data("some data..").code(200)
     * </pre>
     *
     * @return BasePageResponse
     */
    public static <T> BaseResponse<T> builder() {
        return new BaseResponse<T>();
    }

    public Integer getCode() {
        return code;
    }

    public BaseResponse<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public BaseResponse<T> message(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public BaseResponse<T> data(T data) {
        this.data = data;
        return this;
    }

    public Object getExtraParam() {
        return extraParam;
    }

    public BaseResponse<T> extraParam(Object extraParam) {
        this.extraParam = extraParam;
        return this;
    }
    
    /**
     * 构造 BaseResponse<Object> 的静态方法，并默认该次请求返回为成功
     *
     * @return BaseResponse<Object>
     */
    public static BaseResponse<Object> success() {
        Map<String, Object> data = new HashMap<>();
        data.put("success", true);
        return builder().data(data);
    }

    /**
     * 构造 BaseResponse<Object> 的静态方法，并默认该次请求返回为失败
     *
     * @return BaseResponse<Object>
     */
    public static BaseResponse<Object> failed() {
        Map<String, Object> data = new HashMap<>();
        data.put("success", false);
        return builder().data(data);
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setExtraParam(Object extraParam) {
        this.extraParam = extraParam;
    }
}
